package com.sk.bilibili.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sk.bilibili.dto.PraiseInfo;
import com.sk.bilibili.event.BiliBiliEvent;
import com.sk.bilibili.other.R;
import com.sk.bilibili.utils.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Service;
import com.sk.bilibili.bean.Praise;
import com.sk.bilibili.mapper.PraiseMapper;

/**
 * @author 邵康
 * @date 2021/11/25 12:34
 */
@Service
public class PraiseService extends ServiceImpl<PraiseMapper, Praise> {

    @Autowired
    private WarpUtils warpUtils;
    @Autowired
    private TransformUtils transformUtils;
    @Autowired
    private RedisUtils redisUtils;
    @Autowired
    private ApplicationEventPublisher eventPublisher;

    public R<Void> add(Praise praise) {
        save(praise);
        return R.success();
    }

    public R<Void> delete(Integer id) {
        removeById(id);
        return R.success();
    }

    public R<Void> update(Praise praise) {
        updateById(praise);
        return R.success();
    }

    public R<Boolean> switchPraise(Praise praise) {
        praise=transformUtils.transform(praise);
        praise.setMemberId(BSessionUtils.getMemberId());
        Boolean hasPraise = hasPraise(praise.getMemberId(), praise.getType(), praise.getTargetId());
        if(hasPraise){
            Long res = redisUtils.removePraise(praise);
            if(res==null||res==0){//如果不在Redis中必须删除数据库中的
                QueryWrapper<Praise> queryWrapper = QueryWrapperUtils.eq("member_id", praise.getMemberId(),
                        "type", praise.getType(),"target_id", praise.getTargetId());
                remove(queryWrapper);
            }
        }else {
            redisUtils.addPraise(praise);
        }
        eventPublisher.publishEvent(new BiliBiliEvent<>(this,praise).putExtra("add",!hasPraise));
        return R.success(!hasPraise);
    }

    public Boolean hasPraise( int memberId,int type, Integer targetId) {
        Boolean hasPraise = redisUtils.hasPraise(type, targetId, memberId);
        if(hasPraise)return true;//先查询  Redis 再查询数据库(可能不需要查询数据库)
        QueryWrapper<Praise> queryWrapper = QueryWrapperUtils.eq("member_id", memberId, "type", type,
                "target_id", targetId);
        return count(queryWrapper)>0;
    }

    public R<IPage<Praise>> getPraiseVideo(PraiseInfo praiseInfo) {
        if(praiseInfo.getMemberId()==null)praiseInfo.setMemberId(BSessionUtils.getMemberId());
        QueryWrapper<Praise> queryWrapper = QueryWrapperUtils.eq("member_id", praiseInfo.getMemberId());
        queryWrapper.in("type",0,1);
        IPage<Praise> iPage = page(praiseInfo.page(), queryWrapper);
        iPage.getRecords().forEach(praise ->
                praise.getExtra().addExtra(warpUtils.getTypeTarget(praise.getType(),praise.getTargetId())));
        return R.success(iPage);
    }

}
